home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / findsysfolder / findsysfolder.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.4 KB  |  80 lines

  1. /*
  2.     File:        FindSysFolder.c
  3.  
  4.     Contains:    Here's a snippet.  It returns the vRefNum and DirID of the System Folder.
  5.                 It does it the right way under both 6.0 and 7.0.
  6.  
  7.     Written by:     
  8.  
  9.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #include <Types.h>
  25. #include <stdio.h>
  26. #include <OSUtils.h>
  27. #include <Files.h>
  28. #include <Gestalt.h>
  29. #include <Folders.h>
  30. #define BTstQ(arg, bitnbr)        (arg & (1 << bitnbr))
  31.  
  32. /*
  33.     FindSysFolder returns the (real) vRefNum, and the DirID of the current
  34.         system folder.  It uses the Folder Manager if present, otherwise it falls
  35.         back to SysEnvirons.  It returns zero on success, otherwise a standard
  36.         system error.
  37. */
  38. OSErr FindSysFolder(short *fondVRefNum,long *foundDirID);
  39.  
  40. void main()
  41. {
  42.     short vRefNum;
  43.     long DirID;
  44.     FindSysFolder(&vRefNum,&DirID);
  45.     printf("The System Folder is VRefNum:%i DirID:%i\n",vRefNum,DirID);
  46. }
  47. OSErr    FindSysFolder(short *foundVRefNum, long *foundDirID)
  48. {
  49.     long            gesResponse;
  50.     SysEnvRec        envRec;
  51.     WDPBRec            myWDPB;
  52.     unsigned char    volName[34];
  53.     OSErr            err;
  54.  
  55.     *foundVRefNum = 0;
  56.     *foundDirID = 0;
  57.  
  58.     /* Does Folder Manager exist? */
  59.     if (!Gestalt(gestaltFindFolderAttr, &gesResponse) &&
  60.         BTstQ(gesResponse, gestaltFindFolderPresent)) {
  61.         err = FindFolder(kOnSystemDisk, kSystemFolderType, 
  62.             kDontCreateFolder, foundVRefNum, foundDirID);
  63.     } else {    /* Gestalt can't give us the answer, so we resort to SysEnvirons */
  64.         if (!(err = SysEnvirons (curSysEnvVers, &envRec))) {
  65.             myWDPB.ioVRefNum = envRec.sysVRefNum;
  66.             volName[0] = '\0';    /* Zero volume name */
  67.             myWDPB.ioNamePtr = volName;
  68.             myWDPB.ioWDIndex = 0;
  69.             myWDPB.ioWDProcID = 0;
  70.             if (!(err = PBGetWDInfo(&myWDPB, 0))) {
  71.                 *foundVRefNum = myWDPB.ioWDVRefNum;
  72.                 *foundDirID = myWDPB.ioWDDirID;
  73.             }
  74.         }
  75.     }
  76.     return (err);
  77. }
  78. //======================================================================
  79.  
  80.